package org.sunrise.game.common.network.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.Getter;
import org.sunrise.game.common.log.LogCore;
import org.sunrise.game.common.message.MessageUtils;
import org.sunrise.game.common.network.message.BaseMessage;
import org.sunrise.game.common.network.message.MessageType;
import org.sunrise.game.common.network.message.SocketMessage;

@Getter
public class BaseClientPulseHandler extends ChannelInboundHandlerAdapter {
    private final String nodeId;

    public BaseClientPulseHandler(String nodeId) {
        this.nodeId = nodeId;
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            BaseMessage pulseMsg = new BaseMessage(nodeId);
            pulseMsg.setMsg(System.currentTimeMillis());
            ctx.writeAndFlush(new SocketMessage(MessageType.idle, MessageUtils.toBytes(pulseMsg)));
//            LogCore.Client.debug("send ping, cur NodeId = { {} }", nodeId);
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}